package com.lifeservice.console.controller;

import com.lifeservice.common.annotation.Log;
import com.lifeservice.common.core.controller.BaseController;
import com.lifeservice.common.core.domain.AjaxResult;
import com.lifeservice.common.core.page.TableDataInfo;
import com.lifeservice.common.enums.BusinessType;
import com.lifeservice.common.utils.poi.ExcelUtil;
import com.lifeservice.console.domain.LifeOrder;
import com.lifeservice.console.mapper.LifeOrderMapper;
import com.lifeservice.console.service.ILifeOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 生活服务订单Controller
 *
 * @author lifeservice
 * @date 2023-02-28
 */
@RestController
@RequestMapping("/console/order")
public class LifeOrderController extends BaseController {
    @Autowired
    private ILifeOrderService lifeOrderService;
    @Autowired
    private LifeOrderMapper lifeOrderMapper;

    /**
     * 查询生活服务订单列表
     */
    @GetMapping("/list")
    public TableDataInfo list(LifeOrder lifeOrder) {
        List<LifeOrder> list = lifeOrderService.selectLifeOrderList(lifeOrder,false);
        return getDataTable(list);
    }


    /**
     * 核销
     */
    @PutMapping("/declaration")
    public AjaxResult declaration(@RequestBody LifeOrder lifeOrder) {
        return lifeOrderService.declaration(lifeOrder) > 0 ? AjaxResult.success() : AjaxResult.error("核销失败");
    }

    /**
     * 导出生活服务订单列表
     */
    @Log(title = "生活服务订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LifeOrder lifeOrder) {
        List<LifeOrder> list = lifeOrderService.selectLifeOrderList(lifeOrder, true);
        ExcelUtil<LifeOrder> util = new ExcelUtil<LifeOrder>(LifeOrder.class);
        util.exportExcel(response, list, "生活服务订单数据");
    }

    /**
     * 获取生活服务订单详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(lifeOrderService.selectLifeOrderById(id));
    }

    /**
     * 新增生活服务订单
     */
    @Log(title = "生活服务订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LifeOrder lifeOrder) {
        return toAjax(lifeOrderService.insertLifeOrder(lifeOrder));
    }

    /**
     * 修改生活服务订单
     */
    @Log(title = "生活服务订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LifeOrder lifeOrder) {
        return toAjax(lifeOrderService.updateLifeOrder(lifeOrder));
    }

    /**
     * 删除生活服务订单
     */
    @Log(title = "生活服务订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(lifeOrderService.deleteLifeOrderByIds(ids));
    }
}
